<?php

//------------------------------------------------------------------
// File: Event.class.php
// Author: Bryan Frost
// Date Created: 2010-03-25
//
// Event Management
//------------------------------------------------------------------


// Tournament Object
class TournamentData
{
	private $tournamentId;
	private $startDate;
	private $eventsCollection;
	
	
	private $isUSOpenQualifier;
	
	
	protected function setTournamentId($tournamentId)			{ $this->tournamentId = $tournamentId; }
	public function GetTournamentId()							{ return $this->tournamentId; }
	
	protected function setStartDate($startDate)					{ $this->startDate = $startDate; }
	public function GetStartDate()								{ return $this->startDate; }
	
	protected function setEventsCollection($eventsCollection)	{ $this->eventsCollection = $eventsCollection; }
	public function GetEventsCollection()						{ return $this->eventsCollection; }
	
	
	protected function setIsUSOpenQualifier($isUSOpenQualifier)			{ $this->isUSOpenQualifier = $isUSOpenQualifier; }
	public function GetIsUSOpenQualifier()							{ return $this->isUSOpenQualifier; }
	
}



// Core Tournament logic.
class Tournament extends TournamentData
{	
	// Returns collection of all TournamentData
	public function GetAllTournamentData()
	{
		$tournamentDataCollection = array();
		
		$query  = "SELECT Distinct T.TournamentID, T.StartDate FROM Tournaments T ";
		$query .= "INNER JOIN Events E USING (TournamentID) ";
		$query .= "WHERE NumberOfTeams > 0 ";
		$query .= "ORDER BY E.StartDate DESC";
		
		$result = GetQueryResult($query);
		
		while($row = mysqli_fetch_array($result))
		{
			$td = new TournamentData();
			
			$td->setTournamentId($row['TournamentID']);
			$td->setStartDate($row['StartDate']);
			$td->setEventsCollection(Tournament::GetTournamentEvents($td->GetTournamentId()));
			$tournamentDataCollection[] = $td;
		}	
		
		return $tournamentDataCollection;
	}
	
	// NO NEED TO DO THIS, USE EVENTS-SEASON
	public function GetCurrentSeasonTournamentData()
	{
		$tournamentDataCollection = array();
		
		//$query  = "SELECT Distinct T.TournamentID, T.StartDate FROM Tournaments T ";
		$query  = "SELECT Distinct T.TournamentID, T.StartDate, T.IsUSOpenQualifier FROM Tournaments T ";
		$query .= "INNER JOIN Events E USING (TournamentID) ";
		$query .= "WHERE YEAR(T.StartDate) = '2010'";
		$query .= "ORDER BY T.StartDate ASC";
		
		$result = GetQueryResult($query);
		
		while($row = mysqli_fetch_array($result))
		{
			$td = new TournamentData();
			
			
			$td->setIsUSOpenQualifier($row['IsUSOpenQualifier']);
			
			
			$td->setTournamentId($row['TournamentID']);
			$td->setStartDate($row['StartDate']);
			//$td->setEventsCollection(Tournament::GetTournamentEvents($td->GetTournamentId()));
			$td->setEventsCollection(Tournament::GetTournamentEventsPreviewForSchedule($td->GetTournamentId()));
			
			$tournamentDataCollection[] = $td;
		}	
		
		return $tournamentDataCollection;
	}
	
	// Returns TournamentData
	public static function GetTournamentData($tournamentDataId)
	{
		$td = new TournamentData();
		
		$query = "SELECT * FROM Tournaments WHERE TournamentID = $tournamentDataId";
		$result = GetQueryResult($query);
		
		while($row = mysqli_fetch_array($result))
		{
			$td->setTournamentId($row['TournamentID']);
			$td->setStartDate($row['StartDate']);
			$td->setEventsCollection(Tournament::GetTournamentEvents($td->GetTournamentId()));
		}	
		
		return $td;
	}
	
	
	// Returns collection of  aTournament's Events
	private static function GetTournamentEvents($tournamentId)
	{
		$eventCollection = array();
		
		$query = "SELECT EventID FROM Events WHERE TournamentID = $tournamentId";
		
		// TEMPORARY HACK - BLUEHOST IS DOWN
		// TODO: REMOVE WOMEN's OPEN SEPTEMBER 11TH EVENT (tournament changed to QOB)
		$query .= " AND EventID <> 88";


		$result = GetQueryResult($query);
		
		while($row = mysqli_fetch_array($result))
		{
			$eventCollection[] = Event::GetEventPreview($row['EventID']);
		}
		
		return $eventCollection;
	}
	
	
	
	// TODO REMOVE THIS WORKAOURND FOR CURRENT SCHEDULE
	// Returns collection of  aTournament's Events
	private static function GetTournamentEventsPreviewForSchedule($tournamentId)
	{
		$eventCollection = array();
		
		$query = "SELECT EventID FROM Events WHERE TournamentID = $tournamentId";
		$result = GetQueryResult($query);
		
		while($row = mysqli_fetch_array($result))
		{
			$eventCollection[] = Event::GetEventPreviewForSchedule($row['EventID']);
		}
		
		return $eventCollection;
	}
}

?>